import 'dart:math' as math;
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '第01课 - 第一个应用',
      theme: ThemeData(useMaterial3: true, colorSchemeSeed: Colors.blue),
      home: const Lesson01Page(),
    );
  }
}

class Lesson01Page extends StatefulWidget {
  const Lesson01Page({super.key});
  @override
  State<Lesson01Page> createState() => _Lesson01PageState();
}

class _Lesson01PageState extends State<Lesson01Page>
    with TickerProviderStateMixin {
  int count = 0;
  bool colorful = false;

  // 脉动缩放动画
  late final AnimationController _controller = AnimationController(
    vsync: this,
    duration: const Duration(milliseconds: 600),
    lowerBound: 0.95,
    upperBound: 1.05,
  )..repeat(reverse: true);

  // 渐变方向旋转动画（0 -> 2π）
  late final AnimationController _rotation = AnimationController(
    vsync: this,
    duration: const Duration(seconds: 8),
  )..repeat();

  @override
  void dispose() {
    _controller.dispose();
    _rotation.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 根据旋转进度计算渐变起止方向
    final angle = _rotation.value * 2 * math.pi;
    final begin = Alignment(math.cos(angle), math.sin(angle));
    final end = Alignment(-math.cos(angle), -math.sin(angle));

    return Scaffold(
      appBar: AppBar(title: const Text('Hello Flutter')),
      body: Center(
        child: ScaleTransition(
          scale: _controller,
          child: GestureDetector(
            onTap: () => setState(() => colorful = !colorful),
            child: AnimatedContainer(
              width: 180,
              height: 180,
              duration: const Duration(milliseconds: 300),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(24),
                gradient: LinearGradient(
                  begin: begin,
                  end: end,
                  colors: colorful
                      ? [Colors.pink, Colors.orange]
                      : [Colors.blue, Colors.indigo],
                ),
                boxShadow: const [
                  BoxShadow(
                    color: Colors.black26,
                    blurRadius: 12,
                    offset: Offset(0, 8),
                  ),
                ],
              ),
              child: Center(
                child: Text(
                  '点击变色\n$count',
                  textAlign: TextAlign.center,
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() => count++);
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('当前计数: $count')),
          );
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}